<?php

/**
 * DropdownHelper helper library.
 *
 * Helps generating form select options..
 *
 */
class DropdownHelper extends AppHelper {

    private $countryList = array(
        "Africa" => array(
            "DZ" => "Algeria",
            "AO" => "Angola",
            "BJ" => "Benin",
            "BW" => "Botswana",
            "BF" => "Burkina Faso",
            "BI" => "Burundi",
            "CM" => "Cameroon",
            "CV" => "Cape Verde",
            "CF" => "Central African Republic",
            "TD" => "Chad",
            "KM" => "Comoros",
            "CG" => "Congo - Brazzaville",
            "CD" => "Congo - Kinshasa",
            "CI" => "Côte d’Ivoire",
            "DJ" => "Djibouti",
            "EG" => "Egypt",
            "GQ" => "Equatorial Guinea",
            "ER" => "Eritrea",
            "ET" => "Ethiopia",
            "GA" => "Gabon",
            "GM" => "Gambia",
            "GH" => "Ghana",
            "GN" => "Guinea",
            "GW" => "Guinea-Bissau",
            "KE" => "Kenya",
            "LS" => "Lesotho",
            "LR" => "Liberia",
            "LY" => "Libya",
            "MG" => "Madagascar",
            "MW" => "Malawi",
            "ML" => "Mali",
            "MR" => "Mauritania",
            "MU" => "Mauritius",
            "YT" => "Mayotte",
            "MA" => "Morocco",
            "MZ" => "Mozambique",
            "NA" => "Namibia",
            "NE" => "Niger",
            "NG" => "Nigeria",
            "RW" => "Rwanda",
            "RE" => "Réunion",
            "SH" => "Saint Helena",
            "SN" => "Senegal",
            "SC" => "Seychelles",
            "SL" => "Sierra Leone",
            "SO" => "Somalia",
            "ZA" => "South Africa",
            "SD" => "Sudan",
            "SZ" => "Swaziland",
            "ST" => "São Tomé and Príncipe",
            "TZ" => "Tanzania",
            "TG" => "Togo",
            "TN" => "Tunisia",
            "UG" => "Uganda",
            "EH" => "Western Sahara",
            "ZM" => "Zambia",
            "ZW" => "Zimbabwe",
        ),
        "Americas" => array(
            "AI" => "Anguilla",
            "AG" => "Antigua and Barbuda",
            "AR" => "Argentina",
            "AW" => "Aruba",
            "BS" => "Bahamas",
            "BB" => "Barbados",
            "BZ" => "Belize",
            "BM" => "Bermuda",
            "BO" => "Bolivia",
            "BR" => "Brazil",
            "VG" => "British Virgin Islands",
            "CA" => "Canada",
            "KY" => "Cayman Islands",
            "CL" => "Chile",
            "CO" => "Colombia",
            "CR" => "Costa Rica",
            "CU" => "Cuba",
            "DM" => "Dominica",
            "DO" => "Dominican Republic",
            "EC" => "Ecuador",
            "SV" => "El Salvador",
            "FK" => "Falkland Islands",
            "GF" => "French Guiana",
            "GL" => "Greenland",
            "GD" => "Grenada",
            "GP" => "Guadeloupe",
            "GT" => "Guatemala",
            "GY" => "Guyana",
            "HT" => "Haiti",
            "HN" => "Honduras",
            "JM" => "Jamaica",
            "MQ" => "Martinique",
            "MX" => "Mexico",
            "MS" => "Montserrat",
            "AN" => "Netherlands Antilles",
            "NI" => "Nicaragua",
            "PA" => "Panama",
            "PY" => "Paraguay",
            "PE" => "Peru",
            "PR" => "Puerto Rico",
            "BL" => "Saint Barthélemy",
            "KN" => "Saint Kitts and Nevis",
            "LC" => "Saint Lucia",
            "MF" => "Saint Martin",
            "PM" => "Saint Pierre and Miquelon",
            "VC" => "Saint Vincent and the Grenadines",
            "SR" => "Suriname",
            "TT" => "Trinidad and Tobago",
            "TC" => "Turks and Caicos Islands",
            "VI" => "U.S. Virgin Islands",
            "US" => "United States",
            "UY" => "Uruguay",
            "VE" => "Venezuela",
        ),
        "Asia" => array(
            "AF" => "Afghanistan",
            "AM" => "Armenia",
            "AZ" => "Azerbaijan",
            "BH" => "Bahrain",
            "BD" => "Bangladesh",
            "BT" => "Bhutan",
            "BN" => "Brunei",
            "KH" => "Cambodia",
            "CN" => "China",
            "CY" => "Cyprus",
            "GE" => "Georgia",
            "HK" => "Hong Kong SAR China",
            "IN" => "India",
            "ID" => "Indonesia",
            "IR" => "Iran",
            "IQ" => "Iraq",
            "IL" => "Israel",
            "JP" => "Japan",
            "JO" => "Jordan",
            "KZ" => "Kazakhstan",
            "KW" => "Kuwait",
            "KG" => "Kyrgyzstan",
            "LA" => "Laos",
            "LB" => "Lebanon",
            "MO" => "Macau SAR China",
            "MY" => "Malaysia",
            "MV" => "Maldives",
            "MN" => "Mongolia",
            "MM" => "Myanmar [Burma]",
            "NP" => "Nepal",
            "NT" => "Neutral Zone",
            "KP" => "North Korea",
            "OM" => "Oman",
            "PK" => "Pakistan",
            "PS" => "Palestinian Territories",
            "YD" => "People's Democratic Republic of Yemen",
            "PH" => "Philippines",
            "QA" => "Qatar",
            "SA" => "Saudi Arabia",
            "SG" => "Singapore",
            "KR" => "South Korea",
            "LK" => "Sri Lanka",
            "SY" => "Syria",
            "TW" => "Taiwan",
            "TJ" => "Tajikistan",
            "TH" => "Thailand",
            "TL" => "Timor-Leste",
            "TR" => "Turkey",
            "TM" => "Turkmenistan",
            "AE" => "United Arab Emirates",
            "UZ" => "Uzbekistan",
            "VN" => "Vietnam",
            "YE" => "Yemen",
        ),
        "Europe" => array(
            "AL" => "Albania",
            "AD" => "Andorra",
            "AT" => "Austria",
            "BY" => "Belarus",
            "BE" => "Belgium",
            "BA" => "Bosnia and Herzegovina",
            "BG" => "Bulgaria",
            "HR" => "Croatia",
            "CY" => "Cyprus",
            "CZ" => "Czech Republic",
            "DK" => "Denmark",
            "DD" => "East Germany",
            "EE" => "Estonia",
            "FO" => "Faroe Islands",
            "FI" => "Finland",
            "FR" => "France",
            "DE" => "Germany",
            "GI" => "Gibraltar",
            "GR" => "Greece",
            "GG" => "Guernsey",
            "HU" => "Hungary",
            "IS" => "Iceland",
            "IE" => "Ireland",
            "IM" => "Isle of Man",
            "IT" => "Italy",
            "JE" => "Jersey",
            "LV" => "Latvia",
            "LI" => "Liechtenstein",
            "LT" => "Lithuania",
            "LU" => "Luxembourg",
            "MK" => "Macedonia",
            "MT" => "Malta",
            "FX" => "Metropolitan France",
            "MD" => "Moldova",
            "MC" => "Monaco",
            "ME" => "Montenegro",
            "NL" => "Netherlands",
            "NO" => "Norway",
            "PL" => "Poland",
            "PT" => "Portugal",
            "RO" => "Romania",
            "RU" => "Russia",
            "SM" => "San Marino",
            "RS" => "Serbia",
            "CS" => "Serbia and Montenegro",
            "SK" => "Slovakia",
            "SI" => "Slovenia",
            "ES" => "Spain",
            "SJ" => "Svalbard and Jan Mayen",
            "SE" => "Sweden",
            "CH" => "Switzerland",
            "UA" => "Ukraine",
            "SU" => "Union of Soviet Socialist Republics",
            "GB" => "United Kingdom",
            "VA" => "Vatican City",
            "AX" => "Åland Islands",
        ),
        "Oceania" => array(
            "AS" => "American Samoa",
            "AQ" => "Antarctica",
            "AU" => "Australia",
            "BV" => "Bouvet Island",
            "IO" => "British Indian Ocean Territory",
            "CX" => "Christmas Island",
            "CC" => "Cocos [Keeling] Islands",
            "CK" => "Cook Islands",
            "FJ" => "Fiji",
            "PF" => "French Polynesia",
            "TF" => "French Southern Territories",
            "GU" => "Guam",
            "HM" => "Heard Island and McDonald Islands",
            "KI" => "Kiribati",
            "MH" => "Marshall Islands",
            "FM" => "Micronesia",
            "NR" => "Nauru",
            "NC" => "New Caledonia",
            "NZ" => "New Zealand",
            "NU" => "Niue",
            "NF" => "Norfolk Island",
            "MP" => "Northern Mariana Islands",
            "PW" => "Palau",
            "PG" => "Papua New Guinea",
            "PN" => "Pitcairn Islands",
            "WS" => "Samoa",
            "SB" => "Solomon Islands",
            "GS" => "South Georgia and the South Sandwich Islands",
            "TK" => "Tokelau",
            "TO" => "Tonga",
            "TV" => "Tuvalu",
            "UM" => "U.S. Minor Outlying Islands",
            "VU" => "Vanuatu",
            "WF" => "Wallis and Futuna",
        )
    );
    
    function getCountryForCode($code) {
        foreach($this->countryList as $continent => $countries) {
            foreach($countries as $co => $country) {
                if( $code == $co ) {
                    return $country;
                }
            }
        }
        
        return false;
    }

    function generateCountries($dbval = null, $allow_editing = true) {

        foreach ($this->countryList as $continent => $countries) {
            echo "<optgroup label='$continent'>";

            if ($allow_editing) {
                foreach ($countries as $iso => $name) {
                    $selected = $dbval == $iso ? "selected='selected'" : "";
                    echo "<option value='{$iso}' {$selected}>{$name}</option>";
                }
            } else {
                foreach ($countries as $iso => $name) {
                    if ($dbval == $iso) {
                        echo "<option value='{$iso}' >{$name}</option>";
                    }
                }
            }

            echo "</optgroup>";
        }
    }

    function generateState($dbval = NULL, $allow_editing = true) {
        $arr = array(
            'States' => array(
                'AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL',
                'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME',
                'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH',
                'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI',
                'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI',
                'WY'
            ),
            'Provinces' => array(
                'AB', 'BC', 'MB', 'NB', 'NL', 'NS', 'ON', 'PE', 'QC', 'SK',
                'NT', 'NU', 'YT'
            )
        );

        foreach ($arr as $key => $group) {
            echo "<optgroup label='$key'>";

            if ($allow_editing) {
                foreach ($group as $data) {
                    $selected = $dbval == $data ? "selected='selected'" : "";
                    echo "<option value='{$data}' {$selected}>{$data}</option>";
                }
            } else {
                foreach ($group as $data) {
                    if ($dbval == $data) {
                        echo "<option value='{$data}' >{$data}</option>";
                    }
                }
            }

            echo "</optgroup>";
        }
    }

    function generateTitles($dbval = NULL) {
        $arr = array('Mr', 'Mrs', 'Ms');
        foreach ($arr as $data) {
            $selected = $dbval == $data ? "selected='selected'" : "";
            echo "<option value='{$data}' {$selected}>{$data}</option>";
        }
    }

    function generateDays($dbval = NULL) {
        for ($d = 1; $d <= 31; $d++) {
            $selected = $dbval == $d ? "selected='selected'" : "";
            if ($d >= 1 && $d <= 9) {
                $d = '0' . $d;
            }
            echo "<option value='{$d}' {$selected}>{$d}</option>";
        }
    }

    function generateMonths($dbval = NULL) {
        $month_arr = array('01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
            '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
        foreach ($month_arr as $key => $value) {
            $selected = $dbval == $key ? "selected='selected'" : "";
            echo "<option value='{$key}' {$selected}>{$value}</option>";
        }
    }

    function generateMonthsNumric($dbval = NULL) {
        $month_arr = array('01' => '01', '02' => '02', '03' => '03', '04' => '04', '05' => '05', '06' => '06',
            '07' => '07', '08' => '08', '09' => '09', '10' => '10', '11' => '11', '12' => '12');
        foreach ($month_arr as $key => $value) {
            $selected = $dbval == $key ? "selected='selected'" : "";
            echo "<option value='{$key}' {$selected}>{$value}</option>";
        }
    }

    function generateYears($dbval = NULL, $st = NULL, $end = NULL) {
        $year = intval(date('Y'));
        $y = $year - 112;
        $end_yr = $year - 3;

        if ($st) {
            $y = $st;
        }
        if ($end) {
            $end_yr = $end;
        }

        if ($dbval == null) {
            //$dbval = $y;
        }

        for ($y; $y <= $end_yr; $y++) {
            $selected = $dbval == $y ? "selected='selected'" : "";
            echo "<option value='{$y}' {$selected}>{$y}</option>";
        }
    }

    function generateCreditCardTypes($dbval = NULL) {
        /* $arr = array('master'=>'Master Card','amex'=>'American Express'
          ,'visa'=>'Visa','discover'=>'Discover','jcb'=>'JCB','diners'=>'Diners Club'); */
        $arr = array('master' => 'Master Card', /* 'amex' => 'American Express', */ 'visa' => 'Visa');
        foreach ($arr as $key => $data) {
            $selected = $dbval == $key ? "selected='selected'" : "";
            echo "<option value='{$key}' {$selected}>{$data}</option>";
        }
    }

}
